<template>
  <view>
    <!-- 选择收获地址的盒子 -->
    <view class="address-choose-box" v-if="JSON.stringify(address) === '{}'">
      <button type="primary" size="mini" class="address-choose-button" @click="chooseAddress">请选择收获地址+</button>
    </view>
    
    <!-- 渲染收货信息的盒子 -->
    <view class="address-info-box" v-else>
      <view class="row1">
        <view class="row1-left">
          <view class="username">收货人：<text>{{address.userName}}</text></view>
        </view>
        <view class="row1-right">
          <view class="phone">电话：<text>{{address.telNumber}}</text></view>
          <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
      </view>
        <view class="row2">
          <view class="row2-left">收货地址：</view>
          <!-- 计算属性 addressStr -->
          <view class="row2-right">{{addressStr}}</view>
      </view>
    </view>
    <!-- 底部的边框线 -->
    <image src="/static/cart_border@2x.png" class="address-border"></image>
  </view>
</template>

<script>
  import {  mapState, mapMutations,mapGetters} from 'vuex'
  export default {
    name:"my-address",
    data() {
      return {
        // 2.1 注释掉下面的 address 对象，使用 2.2 中的代码替代之
        // address:{}
      };
    },
    methods:{
      // 3.1 把 user 模块中的 updateAddress 函数映射到当前组件
      ...mapMutations('user',['updateAddress']),
      async chooseAddress(){
        const [err, success] =  await uni.chooseAddress().catch(err => err)
        if (err === null && success.errMsg) {
          // 3.2 把下面这行代码注释掉，使用 3.3 中的代码替代之
          // this.address = success
          // 3.3 调用 Store 中提供的 updateAddress 方法，将 address 保存到 Store 里面
          this.updateAddress(success)
          console.log(success)
        }
      }
    },
    computed:{
      // 2.2 把 user 模块中的 address 对象映射当前组件中使用，代替 data 中address 对象
      ...mapState('user',['address']),
      ...mapGetters('user',['addressStr'])
    }
  }
</script>

<style lang="scss">
.address-border{
  display: block;
  width: 100%;
  height: 5px;
}

.address-choose-box{
  height: 90px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.address-info-box{
  font-size: 12px;
  height: 90px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 0 5px;
  .row1{
    display: flex;
    justify-content: space-between;
    .row1-right{
      display: flex;
      justify-content: space-between;
      align-items: center;
      .phone{
        margin-right: 5px;
      }
    }
  }
  .row2{
    display: flex;
    align-items: center;
    margin-top: 10px;
    .row2-left{
      // 文本不换行
      white-space: nowrap;
    }
  }
}
</style>